package com.zaker.touchtv.component;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Outline;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.res.ResourcesCompat;
import android.util.AttributeSet;
import android.util.Property;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.view.animation.DecelerateInterpolator;

import com.zaker.touchtv.R;

public class PlayPauseView extends View {

  private static final Property<PlayPauseView, Integer> COLOR =
      new Property<PlayPauseView, Integer>(Integer.class, "color") {
        @Override
        public Integer get(PlayPauseView v) {
          return v.getColor();
        }

        @Override
        public void set(PlayPauseView v, Integer value) {
          v.setColor(value);
        }
      };

  private static final long PLAY_PAUSE_ANIMATION_DURATION = 200;

  private final PlayPauseDrawable mDrawable;
  private final Paint mPaint = new Paint();
  private final int mPauseBackgroundColor;
  private final int mPlayBackgroundColor;

  private AnimatorSet mAnimatorSet;
  private int mBackgroundColor;
  private int mWidth;
  private int mHeight;

  private AnimatorEndListener mAnimatorEndListener;

  public PlayPauseView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setWillNotDraw(false);
    mBackgroundColor = ResourcesCompat.getColor(getResources(), R.color.colorPublishBtn, null);
    mPaint.setAntiAlias(true);
    mPaint.setStyle(Paint.Style.FILL);
    mDrawable = new PlayPauseDrawable(context);
    mDrawable.setCallback(this);

    mPauseBackgroundColor = ResourcesCompat.getColor(getResources(), R.color.colorPublishBtn, null);
    mPlayBackgroundColor = ResourcesCompat.getColor(getResources(), R.color.colorSubscribeBtn, null);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    final int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
    setMeasuredDimension(size, size);
  }

  @Override
  protected void onSizeChanged(final int w, final int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    //mDrawable.setBounds(0, 0, w, h);
    mWidth = w;
    mHeight = h;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      setOutlineProvider(new ViewOutlineProvider() {
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void getOutline(View view, Outline outline) {
          outline.setOval(0, 0, view.getWidth(), view.getHeight());
        }
      });
      setClipToOutline(true);
    }
  }

  private void setColor(int color) {
    mBackgroundColor = color;
    invalidate();
  }

  public final int getColor() {
    return mBackgroundColor;
  }

  @Override
  protected boolean verifyDrawable(Drawable who) {
    return who == mDrawable || super.verifyDrawable(who);
  }

  @Override
  protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    mPaint.setColor(mBackgroundColor);
    mDrawable.setBounds(0, 0, mWidth, mHeight);
    final float radius = Math.min(mWidth, mHeight) / 2f;
    canvas.drawCircle(mWidth / 2f, mHeight / 2f, radius, mPaint);
    mDrawable.draw(canvas);
  }

  public void setInitState(boolean isPlay) {
    if (mDrawable != null) {
      mDrawable.setInitState(isPlay);
    }
    setColor(isPlay ? mPlayBackgroundColor : mPauseBackgroundColor);
  }

  public PlayPauseDrawable getPlayPauseDrawable() {
    return mDrawable;
  }

  public void toggle() {
    if (mAnimatorSet != null) {
      mAnimatorSet.cancel();
    }

    mAnimatorSet = new AnimatorSet();
    final boolean isPlay = mDrawable.isPlay();
    final ObjectAnimator colorAnim = ObjectAnimator.ofInt(this, COLOR,
        isPlay ? mPauseBackgroundColor : mPlayBackgroundColor);
    colorAnim.setEvaluator(new ArgbEvaluator());
    final Animator pausePlayAnim = mDrawable.getPausePlayAnimator();
    mAnimatorSet.setInterpolator(new DecelerateInterpolator());
    mAnimatorSet.setDuration(PLAY_PAUSE_ANIMATION_DURATION);
    mAnimatorSet.playTogether(colorAnim, pausePlayAnim);
    mAnimatorSet.addListener(new Animator.AnimatorListener() {
      @Override
      public void onAnimationStart(Animator animation) {

      }

      @Override
      public void onAnimationEnd(Animator animation) {
        if (mAnimatorEndListener != null) {
          mAnimatorEndListener.onAnimationEnd();
        }
      }

      @Override
      public void onAnimationCancel(Animator animation) {
        if (mAnimatorEndListener != null) {
          mAnimatorEndListener.onAnimationEnd();
        }
      }

      @Override
      public void onAnimationRepeat(Animator animation) {

      }
    });
    mAnimatorSet.start();
  }

  public void setAnimatorEndListener(AnimatorEndListener animatorEndListener) {
    this.mAnimatorEndListener = animatorEndListener;
  }

  public interface AnimatorEndListener {
    void onAnimationEnd();
  }
}
